package com.ruoyi.student_club.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.student_club.constants.ExamineState;
import com.ruoyi.student_club.dto.ExamineDto;
import com.ruoyi.student_club.dto.JoinDto;
import com.ruoyi.student_club.vo.ExamineVo;
import com.ruoyi.student_club.vo.app.ClubExamineVo;
import org.springframework.stereotype.Service;
import com.ruoyi.student_club.mapper.ClubExamineMapper;
import com.ruoyi.student_club.domain.ClubExamine;
import com.ruoyi.student_club.service.IClubExamineService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 审核管理Service业务层处理
 * 
 * @author 无尤
 * @date 2022-01-24
 */
@Service
public class ClubExamineServiceImpl implements IClubExamineService 
{
    @Resource
    private ClubExamineMapper clubExamineMapper;

    /**
     * 查询审核管理
     * 
     * @param id 审核管理主键
     * @return 审核管理
     */
    @Override
    public ClubExamine selectClubExamineById(Long id)
    {
        return clubExamineMapper.selectClubExamineById(id);
    }

    /**
     * 查询审核管理列表
     * @param clubExamine 审核管理
     * @return 审核管理
     */
    @Override
    public List<ExamineVo> selectClubExamineList(ClubExamine clubExamine) {
        ExamineDto dto = new ExamineDto();
        dto.setName(clubExamine.getName());
        dto.setPhone(clubExamine.getPhone());
        dto.setState(clubExamine.getState());
        dto.setClubId(ShiroUtils.getSysUser().getClubId());
        List<ExamineVo> voList = clubExamineMapper.selectClubExamineList(dto);
        for (ExamineVo vo : voList){
            if (vo.getState().equals(0)){
                vo.setStatus(ExamineState.FAILED.getMsg());
            }
            if (vo.getState().equals(1)){
                vo.setStatus(ExamineState.EXAMINING.getMsg());
            }else {
                vo.setStatus(ExamineState.SUCCESS.getMsg());
            }
        }
        return voList;
    }

    /**
     * 修改审核管理
     * 
     * @param clubExamine 审核管理
     * @return 结果
     */
    @Override
    public int updateClubExamine(ClubExamine clubExamine)
    {
        clubExamine.setUpdateTime(DateUtils.getNowDate());
        return clubExamineMapper.updateClubExamine(clubExamine);
    }

    /**
     * 批量删除审核管理
     * 
     * @param ids 需要删除的审核管理主键
     * @return 结果
     */
    @Override
    public int deleteClubExamineByIds(String ids)
    {
        return clubExamineMapper.deleteClubExamineByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除审核管理信息
     * 
     * @param id 审核管理主键
     * @return 结果
     */
    @Override
    public int deleteClubExamineById(Long id)
    {
        return clubExamineMapper.deleteClubExamineById(id);
    }

    /**
     * 申请加入社团
     * @param dto
     */
    @Override
    public void save(JoinDto dto) {
        ClubExamine clubExamine = clubExamineMapper.checkSame(dto.getUserId(), dto.getClubId());
        if (clubExamine != null){
            throw new RuntimeException("不可重复申请");
        }
        ClubExamine examine = new ClubExamine();
        examine.setUserId(dto.getUserId());
        examine.setClubId(dto.getClubId());
        examine.setName(dto.getName());
        examine.setPhone(dto.getPhone());
        examine.setState(ExamineState.EXAMINING.getCode());
        examine.setCreateTime(new Date());
        examine.setUpdateTime(new Date());
        clubExamineMapper.save(examine);
    }

    /**
     * 获取我的审核状态列表
     * @param userId 用户id
     * @return
     */
    @Override
    public List<ClubExamineVo> getExamineList(Long userId){
        if (userId <= 0){
            throw new RuntimeException("此操作存在注入风险！！！");
        }
        if (userId == null){
            throw new RuntimeException("请先登录！！！");
        }
        List<ClubExamineVo> examineList = clubExamineMapper.getExamineList(userId);
        return examineList;
    }

    /**
     * 获取各个状态列表
     * @param userId 用户id
     * @param state 状态
     * @return
     */
    @Override
    public List<ClubExamineVo> detail(Long userId,Integer state){
        if (userId <= 0){
            throw new RuntimeException("此操作存在注入风险！！！");
        }
        if (userId == null){
            throw new RuntimeException("请先登录！！！");
        }
        List<ClubExamineVo> detail = clubExamineMapper.detail(userId, state);
        return detail;
    }
}
